package it.softphone.cmc.shared.entity;



public class AssetImpl implements Asset {

	private String groupId;
	private String artifactId;
	private String version;

	
	public AssetImpl() {
		super();
	}
	
	public AssetImpl(String groupId, String artifactId, String version) {
		this.groupId = groupId;
		this.artifactId = artifactId;
		this.version = version;
	}

	public final static Asset fromKey( String key ) {
		if( key == null ) throw new IllegalArgumentException( "key is null");
		
		String[] elements = key.split(":");
		
		if( elements.length == 1 ) 
			return new AssetImpl("", elements[0], "");
		if( elements.length == 2 ) 
			return new AssetImpl(elements[0], elements[1], "");
		
		return new AssetImpl(elements[0], elements[1], elements[2]);
	}

	public final static String toKey( Asset asset ) {
		if( asset == null ) throw new IllegalArgumentException("asset is null");
		if( asset.getArtifactId() == null ) throw new IllegalStateException("artifactId is null");
		
		StringBuilder result = new StringBuilder();
		
		if( asset.getGroupId() != null && !asset.getGroupId().isEmpty()) {
			result.append(asset.getGroupId()).append(':');
		}
		if( asset.getArtifactId() != null && !asset.getArtifactId().isEmpty()) {
			result.append(asset.getArtifactId());
		}
		if( asset.getVersion() != null && !asset.getVersion().isEmpty() ) {
			result.append(':').append(asset.getVersion());
		}
		
		return result.toString();		
	}
		
	@Override
	public final String toString() {
		return ( getArtifactId() == null || getArtifactId().isEmpty()) ?  
					"" :	
					toKey(this);
	}

	@Override
	public String getGroupId() {
		return groupId;
	}

	@Override
	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	@Override
	public String getArtifactId() {
		return artifactId;
	}

	@Override
	public void setArtifactId(String artifactId) {
		this.artifactId = artifactId;
	}

	@Override
	public String getVersion() {
		return version;
	}

	@Override
	public void setVersion(String version) {
		this.version = version;
	}

}
